package br.ufal.ic.util;

import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * Classe utilitaria do hibernate.
 * 
 * @author Marcos Paulo
 */
public class HibernateUtil {

    private static final Configuration configuration;
    private static final SessionFactory sessionFactory;

    static {
        try {
            configuration = new AnnotationConfiguration().configure();
            sessionFactory = configuration.buildSessionFactory();

        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public static Transaction beginTransaction() {
        return getCurrentSession().beginTransaction();

    }

    public static void commit() {
        getCurrentSession().getTransaction().commit();
    }

    public static void rollback() {
        getCurrentSession().getTransaction().rollback();
    }
}
